package com.yanqu.road.server.logic.recharge.limit.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.promotion.PromotionInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eQualityType;
import com.yanqu.road.entity.mall.LimitBuyResult;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.module.activity.festivalgift.FestivalGiftModule;
import com.yanqu.road.server.gameplayer.module.patrons.CombLeaderModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.resource.GoodsResourceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.patronsfund.PatronsFundMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.PromotionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PatronsFundMallLimit extends BaseActivityMallLimit {

    public PatronsFundMallLimit(long userId, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(userId, mallInfo, activityInfo);
    }

    @Override
    protected int activityCheck() {
        //判断无双基金活动是否存在
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PatronsFund.getValue());
        if (openActivityInfoList.size() <= 0) {
            return GameErrorCode.E_SELF_PATRONS_NOT_ACTIVITY;
        }
        //充值编号和活动对应关系
        int patronsId = 0;
        String param = GameConfig.ACTIVITY_PEERLESS_NEW_PATRONS_FUND;
        String[] relationStrArr = param.split("\\|");
        boolean isFind = false;
        for (String relationStr : relationStrArr) {
            String[] arr = relationStr.split("=");
            int pId = Integer.parseInt(arr[0]);
            String rechargeIdsStr = arr[1];
            for (String rechargeIdStr : rechargeIdsStr.split(";")) {
                int rId = Integer.parseInt(rechargeIdStr);
                if(mallInfo.getRechargeId() == rId){
                    patronsId = pId;
                    isFind = true;
                    break;
                }
            }
            if(isFind){
                break;
            }
        }
        isFind = false;
        for(ActivityInfo aInfo : openActivityInfoList){
            if(aInfo.getChildType() == patronsId){
                isFind = true;
                activityInfo = aInfo;
                break;
            }
        }
        if(!isFind){
            return GameErrorCode.E_SELF_PATRONS_NOT_ACTIVITY;
        }
        return 0;
    }

    @Override
    protected boolean isSystemUnLock() {
        return true;
    }

    @Override
    protected LimitBuyResult buyCountLimit() {
        return activityMallCountLimit(player);
    }

    @Override
    protected int logicCheck() {
        if (null == activityInfo) {
            return GameErrorCode.E_SELF_PATRONS_NOT_ACTIVITY;
        }
//        //是否购买了旧门客基金
//        long buyOldPatronsFund = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.PatronsFund);
//        if (buyOldPatronsFund != 1) {
//            return GameErrorCode.E_SELF_PATRONS_FUND_NOT_BUY_OLD;
//        }
        //判断是否存在门客
        UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(activityInfo.getChildType());
        if (userPatrons == null) {
            return GameErrorCode.E_SELF_PATRONS_FUND_PATRONS_NOT_EXIST;
        }
        if(userPatrons.getPatronsId() == 102502){//唐三基金加一下联动判断
            if(player.getModule(GoodsResourceModule.class).getPatronsLinkType(userPatrons.getPatronsId()) != 1){
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
        }
        // 获取门客品质
        int quality = PatronsMgr.getPatronsQuality(userPatrons);
        if(quality < eQualityType.Red.getValue()){
            return GameErrorCode.E_SELF_PATRONS_FUND_PATRONS_NOT_EXIST;
        }
        return 0;
    }
}